class PeopleController < ApplicationController
  layout :people_layout
  before_filter :check_authorization,:only=>[:index]

  def people_layout
    if params[:project_id]
      "project"
    else
      "company"
    end
  end
  def index
    @company = @cur_user.company
    if @project
      @us = @company.project_users(params[:project_id]) 
      @us_no = @company.all_users - @company.project_users(params[:project_id]) 
    else
      @us = @company.users - [@company.account_owner]
      @us_no = @company.removed_users
    end
    @user = User.new
  end

  def show
    @user = User.find(params[:id])
  end
  def create_child_company
    @user = User.new
    c = Company.new(params[:child_company])
    c.save
    @cur_user.company.children << c
   render :update do |page|
     page.replace_html 'sub_companies',:partial=>'sub_companies' 
     page.visual_effect :highlight,'company_' + c.id.to_s
     page.visual_effect :blindUp,'new_company'
     page.hide 'indicator_new_company'
   end
  end
  def del_child_company
    @user = User.new
    c = Company.find(params[:id])
    #@cur_user.company.children.remove c
    c.destroy
   render :update do |page|
     page.replace_html 'sub_companies',:partial=>'sub_companies' 
     #page.visual_effect :highlight,'company_' + c.id.to_s
   end
  end
  def create
    @user = User.new(params[:user])
    @company = @cur_user.company
    @user.company_id = @company.id if params[:user][:company_id].nil?
    @user.role = "Employee"
   if @user.save
     if @user.email.match("@")
       Notifier.deliver_notify('signup',@user.email,@user,@user.lang)
     end
    if @project
    task = Task.new
    task.user = @user
    task.project_id  = params[:project_id]
    task.role = "Employee"
    task.save
      @us = @company.project_users(params[:project_id]) - [@company.account_owner]
      @us_no = @company.users - @company.project_users(params[:project_id]) - [@company.account_owner]
    else
      if params[:project_ids]
	params[:project_ids].each do |p|
	  task = Task.new
	  task.project_id = p
	  task.user_id = @user.id
          task.role = "Employee"
          task.save
        end
      end
      @us = @company.users - [@company.account_owner]
      @us_no = @company.removed_users
    end
    render :update do |page|
	  page.visual_effect :BlindUp,'new_user' 
	  page.hide 'indicator'
	  page.replace_html 'sub_companies', :partial=>'sub_companies',:locals=>{:project_id=>params[:project_id]}
	  page.replace_html 'main_company', :partial=>'company',:locals=>{:company=>@cur_user.company,:project_id=>params[:project_id]}
	  page.visual_effect :highlight,'td_' + @user.id.to_s,:duration=>25
	end 
   end 
  end

  def add_person
    task = Task.new
    task.user = User.find(params[:person])
    task.project_id  = params[:project_id]
    task.role = "Employee"
    task.save

    @us = @cur_user.company.project_users(params[:project_id]) - [@cur_user.company.account_owner]
    @us_no = @cur_user.company.users - @us - [@cur_user.company.account_owner]
    render :update do |page|
	  page.visual_effect :BlindUp,'new_user' 
	  page.hide 'indicator2'
	  #page.replace_html 'user_list', :partial=>'user_list',:locals=>{:project_id=>params[:project_id],:company=>}
	  page.replace_html 'sub_companies', :partial=>'sub_companies',:locals=>{:project_id=>params[:project_id]}
	  page.replace_html 'main_company', :partial=>'company',:locals=>{:company=>@cur_user.company,:project_id=>params[:project_id]}
	  #page.replace_html 'us_no', :partial=>'us_no',:locals=>{:us_no=>@us_no,:project_id=>params[:project_id]}
	  page.visual_effect :highlight,'td_' + params[:person].to_s,:duration=>15
    end 
  end

  def remove
    pid = params[:project_id]
    uid = params[:uid]
    Task.find(:first,:conditions=>"user_id = #{uid} and project_id = #{pid}").destroy
    user = User.find(uid)
    #TODO : remove project roles for user 
    #TODO : remove project roles for user 
    #TODO : remove project roles for user 
    #TODO : remove project roles for user 
    @us = @cur_user.company.project_users(pid) - [@cur_user.company.account_owner]
    @us_no =@cur_user.company.users - @us - [@cur_user.company.account_owner]
    render :update do |page|
	  #page.hide 'indicator'
	  page.replace_html 'us_no', :partial=>'us_no',:locals=>{:us_no=>@us_no,:project_id=>pid}
	  page.replace_html 'sub_companies', :partial=>'sub_companies',:locals=>{:project_id=>params[:project_id]}
	  page.replace_html 'main_company', :partial=>'company',:locals=>{:company=>@cur_user.company,:project_id=>params[:project_id]}
          page.replace_html 'cat_side_show',:partial=>'/permission/role_list',:locals=>{:project=>params[:project_id],:label=>'ss'}
    end
  end
  def set_administrator
    user = User.find(params[:id])
    if user.role == "Administrator"
    user.role = "Emaployee"
    else
    user.role = "Administrator"
    end
    user.save
    render :update do |page|
    page.replace_html 'td_' + user.id.to_s, :partial=>'people',:locals=>{:people=>user}
      page.visual_effect :highlight, 'td_' + user.id.to_s, :duration=>'5'
    end
  end

  def change_global_role
    user = User.find(params[:id])
    role = params[:role]
    user.role = role
    user.save
    render :update do |page|
      page.replace_html 'td_' + user.id.to_s, :partial=>'people',:locals=>{:people=>user,:project_id=>params[:project_id]}
      #page.replace_html 'cat_side_show',:partial=>'/permission/role_list',:locals=>{:project=>params[:project_id],:label=>'ss'}
      page.visual_effect :highlight, 'td_' + user.id.to_s, :duration=>'5'
    end
  end
  def change_role
    user = User.find(params[:id])
    pm_role = params[:role]
    task = Task.find(:first,:conditions=>"user_id = #{params[:id]} and project_id = #{params[:project_id]}")
   #project manager role setting
   #project manager setting
   if pm_role
     if task.role_id.to_i > 0
        role = Role.find(task.role_id)
        user.roles.delete(role) if user.roles.include?(role)
     end
     @project.roles.each do |p_role|
       user.roles.delete(p_role) if user.roles.include?(p_role)
     end
     user.save
     task.role = "Project Manager"
     task.role_id = nil
     task.save
   #other role setting
   else
    role = Role.find(params[:role_id])
     #remove all other project roles
     @project.roles.each do |p_role|
       user.roles.delete(p_role) if user.roles.include?(p_role)
     end
    user.roles << role
    task.role = nil 
    task.role_id = params[:role_id]
    task.save
   end 
   #project manager role setting

    render :update do |page|
    page.replace_html 'td_' + user.id.to_s, :partial=>'people',:locals=>{:people=>user,:project_id=>params[:project_id]}
    page.replace_html 'cat_side_show',:partial=>'/permission/role_list',:locals=>{:project=>params[:project_id],:label=>'ss'}
    page.visual_effect :highlight, 'td_' + user.id.to_s, :duration=>'5'
    end
  end

  def edit
    @user = User.find(params[:id])
  end

  def update
    user = User.find(params[:id])
    if user.update_attributes(params[:user])
      if params[:project_id].to_i > 0 
        redirect_to "/projects/#{@project.id}/people"
      else
        redirect_to people_path
      end
    else
      redirect_to edit_person_path(@user)
    end
  end

  def update_company
    @company = Company.find(params[:id])
    if @company.update_attributes(params[:company])
      flash[:notice] = 'Company was successfully updated.'
      redirect_to :action => 'list'
    else
      render :action => 'edit'
    end
  end
  def send_welcome_email
    @user = User.find(params[:id])
    Notifier.deliver_notify('signup',@user.email,@user,@user.lang)
    render :nothing=>true
  end

  def destroyb
    u = User.find(params[:id])
	u.time_logs.each do |t|
	  t.destroy
	end
	u.tasks.each do |t|
	  t.destroy
	end
	u.destroy
    @s_user = session[:user]
	@s_user.reload
	@company = @s_user.company
    @user = User.new
    render :update do |page|
	   page.replace_html 'userlist',:partial=>'user'
    end
  end
  def destroy
    pid = params[:project_id]
    uid = params[:uid]
    u = User.find(params[:uid])
    u.tasks.each do |t|
      t.destroy
    end
    u.roles.clear
    u.destroyed = true
    u.save
    if @project
    @us = @cur_user.company.project_users(@project.id) - [@cur_user.company.account_owner]
    @us_no =@cur_user.company.users - @us - [@cur_user.company.account_owner]
    else
    @us = @cur_user.company.users -  [@cur_user.company.account_owner]
    @us_no = @cur_user.company.removed_users
    end
    render :update do |page|
	  #page.hide 'indicator'
          if @project
	    page.replace_html 'us_no', :partial=>'us_no',:locals=>{:us_no=>@us_no}
          end
	    page.replace_html 'sub_companies', :partial=>'sub_companies',:locals=>{:project_id=>params[:project_id]}
	    page.replace_html 'main_company', :partial=>'company',:locals=>{:company=>@cur_user.company,:project_id=>params[:project_id]}
            page.replace_html 'cat_side_show',:partial=>'/permission/role_list',:locals=>{:project=>params[:project_id],:label=>'ss'} if params[:project_id].to_i > 0 
    end
  end
end
